import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import Dome from './views/dome/Dome.vue'
import Home from './views/dome/Home.vue'

Vue.use(Router)

if(localStorage.getItem("token")){
  store.commit("setToken", localStorage.getItem("token"))
}
if(localStorage.getItem("userInfo")){
  store.commit("setUserInfo", JSON.parse(localStorage.getItem("userInfo")))
}
if(localStorage.getItem("loginInfo")){
  store.commit("setLoginInfo", JSON.parse(localStorage.getItem("loginInfo")))
}

 const router = new Router({
  // 懒加载 一般用于首页或页面结构复杂的页面 解决首次加载慢的问题
  // component: () => import( './views/ ... .vue')
  linkActiveClass: "active",
  mode: "history",
  routes: [
    {
      path: '/',
      name: 'home',
      component: Dome,
      redirect: "Home",
      children:[
        {
          path: "Home",
          name: 'home',
          component: Home,
        },
        {
          path: "Classify",
          name: 'classify',
          component: () => import('./views/dome/Classify.vue'),
        },
        {
          path: "ShoppingCart",
          name: 'shopping_cart',
          component: () => import('./views/dome/ShoppingCart.vue'),
          meta:{requireAuth: true}
        },
        {
          path: "My",
          name: 'my',
          component: () => import('./views/dome/My.vue'),
          meta:{requireAuth: true}
        },
      ]
    },
    {
      path: '/Register',
      name: 'Register',
      component: () => import('./views/register/Register.vue')
    },
    {
      path: '/Login',
      name: 'Login',
      component: () => import('./views/register/Login.vue')
    },
    {
      path: '/Forget',
      name: 'Forget',
      component: () => import('./views/register/Forget.vue')
    },
    {
      path: '/ConfirmOrder',
      name: 'ConfirmOrder',
      component: () => import('./views/dome/ConfirmOrder.vue'),
      meta:{requireAuth: true}
    },
    {
      path: '*',
      component: () => import('./views/Error.vue')
    },
    {
      path: '/goodsDetails',
      name: 'goodsDetails',
      component: () => import('./views/dome/goodsDetails.vue'),
      meta:{requireAuth: true}
    },
    {
      path: '/qilin',
      name: 'qilin',
      component: () => import('./views/dome/qilin.vue'),
      meta:{requireAuth: true}
    },
    {
      path: '/qilinfenlei',
      name: 'qilinfenlei',
      component: () => import('./views/dome/qilinfenlei.vue'),
      meta:{requireAuth: true}
    }
    ,
    {
      path: '/qilinpinpan',
      name: 'qilinpinpan',
      component: () => import('./views/dome/qilinpinpan.vue'),
      meta:{requireAuth: true}
    },
    {
      path: '/qldangan',
      name: 'qldangan',
      component: () => import('./views/dome/qldangan.vue'),
      meta:{requireAuth: true}
    },
    {
      path: '/goodsclass',
      name: 'goodsclass',
      component: () => import('./views/dome/goodsclass.vue'),
      meta:{requireAuth: true}
    },{
      path:'/search',
      name: 'search',
      component: () => import('./views/search/Search.vue'),
      children:[
        {
          path:'/search2',
          name: 'search2',
          component: () => import('./views/search/Search2.vue')
        }
      ]
    },
  ]
})

router.beforeEach((to,from,next) => {
  if(to.meta.requireAuth){
    if(store.state.token){
      next()
    }else{
      next({
        path: "/Login",
        query: {"name" : to.path}
      })
    }
  }else{
    next()
  }
})

export default router